﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Beyondbit.Desktop.Common;
using RBS.Common;
using RBS.Common.JsonEntity;
using Webdiyer.WebControls.Mvc;

namespace RBS.Admin.Areas.User.Controllers
{
    public class ProfileController : RBS.Admin.Controllers.BaseController
    {
  
//list
      public ActionResult UserListIndex(int s=0,int id = 1, string kword = null)
        {

          switch (s)
          {
              case 0:
                  ViewBag.Title = "食客列表";
                  break;
              case 1:
                  ViewBag.Title = "待验证食客";
                  break;
              case 2:
                  ViewBag.Title = "待审核食客";
                  break;
              case 3:
                  ViewBag.Title = "已锁定食客";
                  break;
             default:
                  ViewBag.Title = "食客列表";
                  break; 
          }
        
            const int pageSize = 10;
            ChkAdminLevel("#", RBSEnums.ActionEnum.View.ToString()); //检查权限
            var bll =   new RBS.Core.BLL.U_User();  
           // var admin = GetAdminInfo(); //取得当前管理员信息
            if (kword != null) kword = kword.Replace("'", "");
            var strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(kword))
            {
                strTemp.Append(" and (u_name like  '%" + kword + "%' or nick_name like '%" + kword + "%')");
            }
            string strWhere = "and Status="+s+ strTemp.ToString();
            const string orderby = " ID  desc";
            var startIndex = id;

            int totalItems = 0; //要分页的总记录数
            DataTable dt = bll.GetList(pageSize, startIndex, strWhere, orderby, out totalItems).Tables[0];
            //PagedList构造函数 
            var list = IListDataSet.DataTableToIList<RBS.Core.Model.U_User>(dt);
            PagedList<RBS.Core.Model.U_User> arts = null;
            if(list!=null)
              arts = new PagedList<RBS.Core.Model.U_User>(list, id, pageSize, totalItems);
            else
            {
                list = new List<RBS.Core.Model.U_User>();
                arts = new PagedList<RBS.Core.Model.U_User>(list, id, pageSize, totalItems);  
            } 
            return View(arts);
        }
		//edit
      public ActionResult UserEdit(int id = 0)
        { 
            var model = new RBS.Core.Model.U_User();
            if (id != 0)
            {
               
                var bll = new RBS.Core.BLL.U_User();
                model = bll.GetModel(id);
            } 
            ViewData.Model = model; 
            return View();
        }
        [HttpPost]
        public ActionResult UserEdit(FormCollection collection)
        { 
            var a = collection;
            var model = new  RBS.Core.Model.U_User
                {
                    U_Name =  collection["U_Name"],
                    Nick_Name =  collection["Nick_Name"],
                    U_Uid =  collection["U_Uid"],
                    PassWord =  collection["PassWord"],
                    Email =  collection["Email"],
                    Telphone =  collection["Telphone"],
                    Mobile =  collection["Mobile"],
                    Sex =  collection["Sex"],
                    Birthday = DateTime.Parse(collection["Birthday"]),
                    QQ =  collection["QQ"],
                    Exp = Convert.ToInt32(collection["Exp"]),
                    Status = Convert.ToInt32(collection["Status"]),
                    Reg_Time =  DateTime.Now,
                    Reg_IP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
                }; 
            
            var bll = new RBS.Core.BLL.U_User();
            if (collection["Id"] != "0")
            {
                model.Id = Convert.ToInt32(collection["Id"]);
                bll.Update(model);
                AddAdminLog(RBSEnums.ActionEnum.Edit.ToString(), "修改:" + model.Id); //记录日志
            }
            else
            {
              
                bll.Add(model);
                AddAdminLog(RBSEnums.ActionEnum.Add.ToString(), "添加:" + model.Id); //记录日志
            }
            return RedirectToAction("UserEdit");
        } 
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Delete(string ids, string returnUrl)
        {
            var ret = new JsonReturnMessages();
            ChkAdminLevel("#", RBSEnums.ActionEnum.Delete.ToString()); //检查权限 
            try
            {
                foreach (var id in ids.TrimEnd(',').Split(',').ToList())
                {
                    new RBS.Core.BLL.U_User().Delete(int.Parse(id));
                }
            }
            catch (Exception exception)
            {
                ret.IsSuccess = false;
                ret.Msg = "操作失败！";
                return Json(ret);
            }
            AddAdminLog(RBSEnums.ActionEnum.Delete.ToString(), "删除"); //记录日志
            ret.IsSuccess = true;
            ret.Msg = "操作成功";
            ret.Data = !string.IsNullOrEmpty(returnUrl) ? returnUrl : Url.Action("AdminUserManager", "Sys");
            return Json(ret);
        }
          
 
    }
}
